﻿<?php
class Backlogin extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper("url");
		$this->load->database();
		$this->load->library('session');
		@session_start();
	}
	
	function index(){
		$this->load->view("backstage/login.html");
	}
	
	function check_login(){
		
		
		$yanzheng = $this->input->post("verifycode");
		if($_SESSION['authcode']){//判断是否存在验证码的session，不存在则返回
			$true_yanzheng = $_SESSION['authcode']; //获得验证码
			$_SESSION['authcode'] = "";//销毁验证码的session
			$username = $this->input->post("username");
					$password = $this->input->post("password");
			if($true_yanzheng != $yanzheng || strlen($yanzheng) != 5){        //首先判断验证码是否正确，或者长度是否合法，如果错误直接报错，如果正确在进行下一步 
				$data['error']="验证码错误！";
				$this->load->view("backstage/login.html",$data);
			}else if(strlen($username) > 20){//用户名过长的话，报错
						$data['error']="用户名或密码错误";
						$this->load->view("backstage/login.html",$data);
			}else{
					$this->load->model("Backstage_model");
					$admin_message=$this->Backstage_model->get_admin_message($username);
					if($admin_message){
								$id=$admin_message['id'];
								$username1=$admin_message['username'];
								$password1=$admin_message['password'];
						}
					if(empty($admin_message)){
						$data['error']="用户名或密码错误";
						$this->load->view("backstage/login.html",$data);
					}else if(md5($password)!=$password1){
						$data['error']="用户名或密码错误";
						$this->load->view("backstage/login.html",$data);
					}
					else{
						$this->session->set_userdata("id",$id);
						$this->session->set_userdata("username",$username1);
						$this->session->set_userdata("admin",'ok');
						redirect("backstage/index");
					}
				}
		}else{
			$this->load->view("backstage/login.html");
		}
	}
	
	function quit(){
		$this->session->sess_destroy();   //销毁session
		$this->load->view("backstage/login.html");
	}
	
	function verifycode(){
			//验证码生成方法
		ob_clean();
		Header("Content-type: image/PNG");
		srand((double)microtime()*1000000);			//播下一个生成随机数字的种子，以方便下面随机数生成的使用
		$string = "0123456789";
		//$string = "abcdefghijklmnopqrstuvwxyz0123456789";
		$newstr = "";
		$strlength = strlen($string);
		for($i=1;$i<=5;$i++){
			$charat = rand(0,($strlength-1));
			$char = substr($string,$charat,1);
			$newstr .= $char;
		}
		$_SESSION['authcode']="";
		$im = imagecreate(62,20); 					//制定图片背景大小

		$black = ImageColorAllocate($im, 0,0,0);	//设定三种颜色
		$white = ImageColorAllocate($im, 255,255,255);
		$gray = ImageColorAllocate($im, 200,200,200);

		imagefill($im,0,0,$gray); 					//采用区域填充法，设定（0,0）

		while(($authcode=rand()%100000)<10000);
		//将四位整数验证码绘入图片
		$_SESSION['authcode']=$newstr;
		imagestring($im, 5, 10, 3, $newstr, $black);
		// 用 col 颜色将字符串 s 画到 image 所代表的图像的 x，y 座标处（图像的左上角为 0, 0）。
		//如果 font 是 1，2，3，4 或 5，则使用内置字体

		for($i=0;$i<200;$i++)   					//加入干扰象素
		{
			$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
			imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
		}
		ImagePNG($im);
		ImageDestroy($im);

	}
	
	
	
	
	
	
		
	
	
	
	
}?>